home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 2008 Pearl Crescent, LLC. All Rights Reserved. */
- /* vim: set sw=2 sts=2 ts=8 et syntax=javascript: */
-
- var gAviaryConnect = {
- kPearlUtilJSURI: "chrome://aviary/content/pearlutil.js",
-
- kCapturedImageExpireTimeMS: 300000, // 5 minutes (5 * 60 * 1000)
- kMaxStackDepth: 250,
-
- // Use lowercase letters within mHosts.
- mHosts: [ "aviary.com", "test.viary.com", "test.aviary.com" ],
- mCurrentID: 0,
- mCapturedImages: null,
-
- _mIOSvc: null,
-
- // nsISupports implementation.
- QueryInterface: function (aIID)
- {
- if (!aIID.equals(Components.interfaces.nsISupports) &&
- !aIID.equals(Components.interfaces.nsIFactory) &&
- !aIID.equals(Components.interfaces.nsIClassInfo) &&
- !aIID.equals(Components.interfaces.aviaryIConnectInternal) &&
- !aIID.equals(Components.interfaces.aviaryIConnect))
- {
- throw Components.results.NS_ERROR_NO_INTERFACE;
- }
-
- return this;
- },
-
- // nsIFactory implementation.
- createInstance: function (aOuter, aIID)
- {
- if (null != aOuter)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
-
- // Load Pearl Utility library.
- var jsLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
- .createInstance(Components.interfaces.mozIJSSubScriptLoader);
- jsLoader.loadSubScript(this.kPearlUtilJSURI);
-
- this.mPearlUtil = com.aviary.talon.pearlutil;
-
- return this.QueryInterface(aIID);
- },
-
- lockFactory: function (aDoLock) {},
-
- // nsIClassInfo implementation.
- getInterfaces: function(aCount)
- {
- // Whatever interfaces are listed here are available to web pages.
- var iList = [ Components.interfaces.nsIClassInfo,
- Components.interfaces.aviaryIConnect ];
- aCount.value = iList.length;
- return iList;
- },
-
- getHelperForLanguage: function (aLanguage)
- {
- return null;
- },
-
- classDescription: "AviaryConnectAPI",
- implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
- flags: Components.interfaces.nsIClassInfo.DOM_OBJECT,
-
- // aviaryIConnectInternal implementation.
- addCapturedImage: function(aMimeType, aDataURI)
- {
- this.deleteOldImages();
-
- if (!aMimeType || !aDataURI)
- throw Components.results.NS_ERROR_INVALID_ARG;
-
- ++this.mCurrentID;
- // dump("addCapturedImage: " + aMimeType + ", ID: " + this.mCurrentID + "\n");
- var imageObj = new Object;
- imageObj.id = this.mCurrentID;
- imageObj.mimeType = aMimeType;
- imageObj.dataURI = aDataURI;
- imageObj.timeAddedMS = Date.now();
-
- if (!this.mCapturedImages)
- this.mCapturedImages = new Object;
- this.mCapturedImages[imageObj.id] = imageObj;
-
- return imageObj.id;
- },
-
- // aviaryIConnect implementation.
- hasClipboardData: function(aMimeType)
- {
- this.checkAccess("hasClipboardData");
-
- return this.mPearlUtil.HasClipboardData(aMimeType);
- },
-
- getClipboardData: function(aMimeType)
- {
- this.checkAccess("getClipboardData");
-
- return this.mPearlUtil.GetClipboardAsDataURI(aMimeType);
- },
-
- getCapturedImageMimeType: function(aID)
- {
- // dump("getCapturedImageMimeType: " + aID + "\n");
- this.checkAccess("getCapturedImageMimeType");
-
- // Delete old images first since we do not return a MIME type if we
- // are about to delete the image.
- this.deleteOldImages();
-
- if (aID && this.mCapturedImages)
- {
- var imageObj = this.mCapturedImages[aID];
- if (imageObj)
- return imageObj.mimeType;
- }
-
- return null;
- },
-
- getCapturedImage: function(aID, aDeleteIt)
- {
- // dump("getCapturedImage: " + aID + ", delete id: " + aDeleteIt + "\n");
- this.checkAccess("getCapturedImage");
-
- var dataURI = null;
- if (aID && this.mCapturedImages)
- {
- var imageObj = this.mCapturedImages[aID];
- if (imageObj)
- {
- dataURI = imageObj.dataURI;
- if (aDeleteIt)
- delete this.mCapturedImages[aID];
- }
- }
-
- this.deleteOldImages();
- return dataURI;
- },
-
- deleteCapturedImage: function(aID)
- {
- // dump("deleteCapturedImage: " + aID + "\n");
- this.checkAccess("deleteCapturedImage");
-
- if (aID && this.mCapturedImages)
- {
- var imageObj = this.mCapturedImages[aID];
- if (imageObj)
- delete this.mCapturedImages[aID];
- }
-
- this.deleteOldImages();
- },
-
- // internal functions.
- deleteOldImages: function()
- {
- // dump("deleteOldImages\n");
- if (this.mCapturedImages)
- {
- var curTimeMS = Date.now();
- for each (var imageObj in this.mCapturedImages)
- {
- // dump(" checking image: " + imageObj.id + "\n");
- if ((curTimeMS - imageObj.timeAddedMS) >
- this.kCapturedImageExpireTimeMS)
- {
- // dump(" deleting old image: " + imageObj.id + "\n");
- delete this.mCapturedImages[imageObj.id];
- }
- }
- }
- },
-
- // Check that web page has access and throw an error if not.
- checkAccess: function(aDesc)
- {
- var isURLOK = false;
-
- // Find top-most JS stack frame.
- var stackFrame = Components.stack;
- for (var i = 0; i < this.kMaxStackDepth; ++i)
- {
- if (!stackFrame.caller)
- break;
-
- stackFrame = stackFrame.caller;
- }
-
- // Verify that URL is white listed.
- if (stackFrame && !stackFrame.caller)
- {
- // dump("checkAccess " + aDesc + "- URL: " + stackFrame.filename + "\n");
- isURLOK = this.isURLWhiteListed(stackFrame.filename);
- }
-
- // dump("checkAccess " + aDesc + " - " + (isURLOK ? "OK" : "Denied") + "\n");
-
- if (!isURLOK)
- {
- throw new Components.Exception("access denied",
- Components.results.NS_ERROR_FAILURE);
- }
- },
-
- isURLWhiteListed: function(aURL)
- {
- if (!aURL)
- return false;
-
- try
- {
- var uriObj = this.mIOSvc.newURI(aURL, null, null);
- var scheme = uriObj.scheme;
-
- // if ("chrome" == scheme)
- // return true; // allow access from all chrome pages.
-
- var lcHost = uriObj.host.toLowerCase();
- if ("http" == scheme || "https" == scheme)
- {
- var len = this.mHosts.length;
- for (var i = 0; i < len; ++i)
- {
- if (lcHost == this.mHosts[i])
- return true; // allowed access from white listed host.
- }
- }
- } catch (e) {}
-
- return false;
- },
-
- get mIOSvc()
- {
- if (!this._mIOSvc)
- {
- this._mIOSvc = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
- }
-
- return this._mIOSvc;
- },
-
- endOfObject: true
- };
-
- var gAviaryConnectModule =
- {
- kICompReg: Components.interfaces.nsIComponentRegistrar,
- kClassID: Components.ID("{6FCCDA4C-4EA0-4204-B0AA-4E9546147AA0}"),
- kContractID: "@aviary.com/aviary-api;1",
- kDesc: "Aviary Connect API Module",
- kJSName: "aviaryconnect",
- kJSGlobalPropCategory: "JavaScript global property",
-
- // nsISupports implementation.
- QueryInterface: function (aIID)
- {
- if (aIID.equals(Components.interfaces.nsIModule) ||
- aIID.equals(Components.interfaces.nsISupports))
- return this;
-
- throw Components.results.NS_ERROR_NO_INTERFACE;
- },
-
- // nsIModule implementation.
- getClassObject: function (aCompMgr, aClassID, aIID)
- {
- if (!aClassID.equals(this.kClassID))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- if (!aIID.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- return gAviaryConnect.QueryInterface(aIID);
- },
-
- registerSelf: function (aCompMgr, aFileSpec, aLocation, aType)
- {
- aCompMgr = aCompMgr.QueryInterface(this.kICompReg);
- aCompMgr.registerFactoryLocation(this.kClassID, this.kDesc,
- this.kContractID, aFileSpec, aLocation, aType);
-
- var catMgr = Components.classes["@mozilla.org/categorymanager;1"]
- .getService(Components.interfaces.nsICategoryManager);
- catMgr.addCategoryEntry(this.kJSGlobalPropCategory, this.kJSName,
- this.kContractID, true, true);
- },
-
- unregisterSelf: function (aCompMgr, aFileSpec, aLocation)
- {
- var catMgr = Components.classes["@mozilla.org/categorymanager;1"]
- .getService(Components.interfaces.nsICategoryManager);
- catMgr.deleteCategoryEntry(this.kJSGlobalPropCategory, this.kJSName,
- true);
- aCompMgr = aCompMgr.QueryInterface(this.kICompReg);
- aCompMgr.unregisterFactoryLocation(this.kClassID, aFileSpec);
- },
-
- canUnload: function (aCompMgr) { return true; },
-
- endOfObject: true
- };
-
-
- function NSGetModule(aCompMgr, aFileSpec)
- {
- return gAviaryConnectModule;
- }
-
-